//
//  CustomNavView.m
//  Doctor
//
//  Created by 阳光 on 16/9/28.
//  Copyright © 2016年 阳光. All rights reserved.
//

#import "CustomNavView.h"

@implementation CustomNavView

-(instancetype)initWithTitle:(NSString *)title leftBtnImage:(NSString *)leftBtnImage hasLeftBtnImage:(BOOL)hasLeftBtnImage    rightBtnTitle:(NSString *)rightBtnTitle hasRightBtnTitle:(BOOL)hasRightBtnTitle
{
    if (self=[super init])
    {
        self.navTitle=title;
        self.navTitleLabel.textColor=Color000000;
        self.backgroundColor=WhiteColor;
        
        self.bottomLine.hidden=NO;
        
        if (hasLeftBtnImage)
        {
            self.leftBtn.hidden=NO;
            [self.leftBtn setImage:[UIImage imageNamed:leftBtnImage] forState:0];
        }
        
        if (hasRightBtnTitle)
        {
            self.rightBtn.hidden=NO;
            [self.rightBtn setTitle:rightBtnTitle forState:0];
            [self.rightBtn setTitleColor:Color000000 forState:0];
            self.rightBtn.titleLabel.font=Font_Number(16);
        }
    }
    return self;
}

// 预防
- (instancetype)init {
    if (self = [super init]) {
        NSAssert1(NO, @"%s  必须使用initWithTitle:方法初始化", __FUNCTION__);
    }
    return self;
}

-(UILabel *)navTitleLabel
{
    if (!_navTitleLabel) {
        _navTitleLabel=[UILabel new];
        [self addSubview:_navTitleLabel];
        [_navTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.mas_centerX);
            make.bottom.mas_equalTo(-kmargin);
        }];
        _navTitleLabel.font=Font_Number(16);
        _navTitleLabel.text=_navTitle;
    }
    return _navTitleLabel;
}

-(UIButton *)leftBtn
{
    if (!_leftBtn) {
        _leftBtn=[UIButton new];
        [self addSubview:_leftBtn];
        [_leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(kmargin);
            make.centerY.mas_equalTo(self.navTitleLabel.mas_centerY);
        }];
        [_leftBtn addTarget:self action:@selector(leftBtnEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftBtn;
}

-(UIButton *)rightBtn
{
    if (!_rightBtn) {
        _rightBtn=[UIButton new];
        [self addSubview:_rightBtn];
        [_rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-kmargin);
            make.centerY.mas_equalTo(self.navTitleLabel.mas_centerY);
        }];
    [_rightBtn addTarget:self action:@selector(rightBtnEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightBtn;
}

-(UIView *)bottomLine
{
    if (!_bottomLine) {
        _bottomLine=[UIView new];
        _bottomLine.backgroundColor=MAIN_BACK_COLOR;
        [self addSubview:_bottomLine];
        [_bottomLine mas_makeConstraints:^(MASConstraintMaker *make)
        {
            make.bottom.mas_equalTo(0);
            make.size.mas_equalTo(CGSizeMake(APPW,1));
        }];
    }
    return _bottomLine;
}

-(void)leftBtnEvent
{
    if ([self.delegate respondsToSelector:@selector(leftBtnEvent)])
    {
        [self.delegate leftBtnEvent];
    }
}

-(void)rightBtnEvent
{
    if ([self.delegate respondsToSelector:@selector(rightBtnEvent)])
    {
        [self.delegate rightBtnEvent];
    }
}

@end
